import Vue from 'vue'
import store from './store'
import App from './App'

Vue.config.productionTip = false
// 后端api地址
Vue.prototype.$unishow = "https://admin.dqzjw.cn/api/";
Vue.prototype.$platform = 'h5';
Vue.prototype.$store = store;
Vue.prototype.$ver = '1.0.0';
// 提示
var msgTimer=null;
const msg = (title, duration = 3000, mask = false, icon = 'none') => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
	if(msgTimer){
		clearTimeout(msgTimer)
	}
	msgTimer=setTimeout(function() {
		uni.hideToast();
	}, duration)
}

// 返回上一页
const prePage = () => {
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}
// 复制
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)
// 跨域调接口
import {VueJsonp} from 'vue-jsonp'
Vue.use(VueJsonp)
// 成功不会弹出 失败才会
const request = async (url, method = 'GET', data = {}, showMsg = false) => {
	let header = {
		'content-type': 'application/x-www-form-urlencoded',
		'platform': Vue.prototype.$platform,
		'ver': Vue.prototype.$ver,
	};
	if (Vue.prototype.$store.state.userInfo.token) {
		header.token = Vue.prototype.$store.state.userInfo.token;
	}
	if (Vue.prototype.$store.state.cookie) {
		header.cookie = Vue.prototype.$store.state.cookie;
	}
	var [error, res] = await uni.request({
		url: Vue.prototype.$unishow + url,
		method: method,
		header: header,
		data: data,
		timeout: 5000
	});

	return new Promise(function(revolve) {
		if (error) {
			showMsg && msg(JSON.stringify(res));
			revolve(false);
		}

		if (res) {
			if (res.header.hasOwnProperty('Set-Cookie')) {
				let cookie = res.header['Set-Cookie'].replace("; path=/", "");
				Vue.prototype.$store.commit('setCookie', cookie);
			}
			if (res.hasOwnProperty('data')) {
				if (res.data.hasOwnProperty('code') && res.data.code == 401) {
					// 未登录 或 登录失效
					Vue.prototype.$store.commit('logout');
				}
				if (res.data.status == 'SUCCESS' || res.data.code == 1) {
					revolve(res.data);
				} else {
					showMsg && msg(res.data.msg);
					revolve(res.data);
				}
			} else {
				showMsg && msg('不能识别数据');
				revolve(false);
			}
		}
	});

}

Vue.config.productionTip = false
Vue.prototype.$fire = new Vue();
Vue.prototype.$api = {
	msg,
	prePage,
	request,
	navTo
};
// 跳转判断是否登录
const navTo = (url, check = true) => {

	uni.navigateTo({
		url: url
	});
}

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
//数字滚动
import countUp from '@/common/countUp.js'
Vue.prototype.countUp = countUp;
// 时间格式转换
Vue.prototype.$setTime = function(time,type=1) {
	if (!time || time == 0) {
		return 0
	}
	let date = new Date(time * 1000); //如果date为13位不需要乘1000
	let Y = date.getFullYear() + '-';
	let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
	let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
	let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
	let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	if(type==2){
		return Y + M + D;
	}
	return Y + M + D + h + m + s;
}
Vue.prototype.$globalData = {
  openid: null
};
// 获取app内用户信息
Vue.prototype.$getH5UserInfo = function(){
	return new Promise((resolve)=>{
		try{
				resolve({'mobile':'15882318565','id':'1004398'})
				// resolve({'mobile':'15882318565','id':'1004398'})
			 // resolve({'mobile':'18838000395','id':'1056724'})
			// resolve({'mobile':'17608041106','id':'110159'})
			// return
			// getH5UserInfo js文件内置方法
			// getH5UserInfo().then((res) => {
			// 	if (res) {
			// 		resolve(JSON.parse(res))
			// 		return
			// 	}
			// 	resolve(false)
			// })
		}catch(e){
		}
	})
};